<template>
  <div class="app-container">
    <!--
      上传excel组件
      属性on-success - 当成功的时候触发
      属性before-upload - 上传前
     -->
    <son-comp :money="100" :fn="fn" />
    <upload-excel :on-success="handleSuccess" :before-upload="beforeUpload" />
    <el-table :data="tableData" border highlight-current-row style="width: 100%;margin-top:20px;">
      <el-table-column v-for="item of tableHeader" :key="item" :prop="item" :label="item" />
      <!-- v-for of 拿到的每一项  v-for in 拿到的下标 -->
    </el-table>
  </div>
</template>

<script>
import SonComp from './components/son-comp.vue'

export default {
  components: { SonComp },

  data() {
    return {
      tableData: [],
      tableHeader: []
    }
  },
  methods: {
    fn({ results, header }) {
      console.log('执行了吗', results, header)
    },
    // 上传前校验文件的大小 默认的单位是b
    beforeUpload(file) {
      // console.log(file, file.size / 1024 / 1024)
      const isLt1M = file.size / 1024 / 1024 < 1
      if (isLt1M) {
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    handleSuccess({ results, header }) {
      // result 表格数据 header 表格头部名称
      // console.log(results, header)
      this.tableData = results
      this.tableHeader = header
    }
  }
}
</script>
